﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Xml;

/// <summary>
/// Summary description for DataLoader
/// </summary>
public class DataLoader
{
    private XmlDocument xmlDoc;
    private List<Automation> automationList;
    private DataLoader dataLoader;
	public DataLoader( string fileName )
	{
        if( dataLoader == null )
        {
            xmlDoc = new XmlDocument();
            xmlDoc.Load(fileName);
            Load();
        }
	}

    public List<Automation> GetAllAutomations()
    {
        return automationList;
    }

    public Automation GetAutomationById( int id )
    {
        foreach (var autoApp in automationList)
        {
            if (autoApp.Id == id)
                return autoApp;
        }
        return null;
    }

    private void Load()
    {
        automationList = new List<Automation>();

        if (xmlDoc.DocumentElement != null)
        {
            foreach (XmlElement appNode in xmlDoc.DocumentElement.ChildNodes)
            {
                Automation automation = new Automation();
                automation.Id = Convert.ToInt32(appNode.GetAttribute("id"));
                foreach (XmlElement propertyNode in appNode.ChildNodes)
                {
                    switch (propertyNode.Name.ToUpper())
                    {
                        case "NAME":
                            automation.Name = propertyNode.InnerText;
                            break;
                        case "REGION":
                            automation.Region = propertyNode.InnerText;
                            break;
                        case "SECTOR":
                            automation.Sector = propertyNode.InnerText;
                            break;
                        case "DESCRIPTION":
                            automation.Description = propertyNode.InnerXml;
                            break;
                        case "Measurement":
                            automation.Measurements = LoadMeasurement(propertyNode);
                            break;
                    }
                }
                automationList.Add(automation);
            }
        }
    }

    private List<Measurement> LoadMeasurement( XmlElement measurementNode )
    {
        List<Measurement> measurements = new List<Measurement>();
        foreach (XmlElement alertNode in measurementNode.ChildNodes)
        {
            Measurement measurement = new Measurement();
            foreach (XmlElement propertyNode in alertNode.ChildNodes)
            {
                switch (propertyNode.Name.ToUpper())
                {
                    case "Content":
                        measurement.Alert = propertyNode.InnerText;
                        break;
                    case "Time":
                        measurement.Time = propertyNode.InnerText;
                        break;
                    case "Competitor":
                        measurement.Competitor = propertyNode.InnerText;
                        break;
                    case "CompetitorTime":
                        measurement.CompetitorTime = propertyNode.InnerText;
                        break;
                    case "Result":
                        measurement.Result = propertyNode.InnerText;
                        break;
                }
            }
            measurements.Add(measurement);
        }
        return measurements;
    }
}
